package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IRoomTypePriceDao;
import com.eling.elcms.travel.model.RoomTypePrice;
import com.eling.elcms.travel.model.view.SumRoomPrice;
import com.eling.elcms.travel.service.IRoomTypePriceManager;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RoomTypePriceManagerImpl extends GenericManagerImpl<RoomTypePrice, Long> implements IRoomTypePriceManager{

	IRoomTypePriceDao roomTypePriceDao;

	@Autowired
	public void setRoomTypePriceDao(IRoomTypePriceDao RoomTypePriceDao) {
		this.dao = RoomTypePriceDao;
		this.roomTypePriceDao = RoomTypePriceDao;
	}

	@Override
	public List<RoomTypePrice> deleteAndSave(RoomTypePrice roomTypePrice, Date startDate, Date endDate, String week) {
		
		String weekStr = "";
		String[] weekArr = week.split(",");
		for(int i=0;i<weekArr.length;i++){
			if(i == weekArr.length-1){
				weekStr += "'"+weekArr[i]+"'";
			}else{
				weekStr += "'"+weekArr[i]+"',";	
			}
		}
		
		//先将之前数据删除
		roomTypePriceDao.deleteOldPrice(roomTypePrice,startDate,endDate,weekStr);
		
		roomTypePrice.setCreateDate(new Date());
		roomTypePrice.setCreater((CommonUser)AppContext.curUser());
		
		List<RoomTypePrice> list = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		while(!sdf.format(startDate).equals(sdf.format(endDate)) && startDate.getTime()<endDate.getTime()){
			//判断开日期到结束日期 不含结束日期 是否为所选星期
			if(week.indexOf(getWeekOfDate(startDate)) != -1){
				RoomTypePrice newRTP = new RoomTypePrice();
				PropertyUtils.copyProperties(newRTP, roomTypePrice);
				newRTP.setSaleDate(startDate);
				list.add(newRTP);
			}
			startDate = DateUtils.addDays(startDate, 1);
		}
		//判断结束日期是否为选中星期
		if(week.indexOf(getWeekOfDate(endDate)) != -1){
			RoomTypePrice newRTP = new RoomTypePrice();
			PropertyUtils.copyProperties(newRTP, roomTypePrice);
			newRTP.setSaleDate(endDate);
			list.add(newRTP);
		}
		return save(list);
	}
	public static String getWeekOfDate(Date dt) {
	     String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	     Calendar cal = Calendar.getInstance();
	     cal.setTime(dt);
	     int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
	     if (w < 0){
	    	 w = 0;
	     }
	     return weekDays[w];
	}

	@Override
	public SumRoomPrice querySumRoomPrice(RoomTypePrice cond) {
		SumRoomPrice sumRoomPrice = new SumRoomPrice();
		Double sumVipPrice = 0.0;
		Double sumStandardPrice = 0.0;
		Double sumProtocolPrice = 0.0;
		List<RoomTypePrice>  list = query(cond);
		for(RoomTypePrice roomTypePrice : list){
			sumVipPrice+=roomTypePrice.getVipPrice();
			sumStandardPrice+=roomTypePrice.getStandardPrice();
			sumProtocolPrice+=roomTypePrice.getProtocolPrice();
		}
		sumRoomPrice.setSumProtocolPrice(sumProtocolPrice);
		sumRoomPrice.setSumStandardPrice(sumStandardPrice);
		sumRoomPrice.setSumVipPrice(sumVipPrice);
		return sumRoomPrice;
	}
}
